<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>One To One - Join Table</title>
</head>
<body>
  <h3>One To One - Using Join Table</h3>
  One to one association with a join table is quite an exotic mapping, but can be useful while mapping an optional relationship.
  For example, let's say we are mapping an Employee to Desk association for a Company XYZ. XYZ has a very relaxed policy on
  employee sitting arrangement. An employee can sit on any desk or he can work from home, therefore making the relationship optional.
  Also, note that Employee and Desk are two totally independent entities and the relationship between them is controlled by Employee
  entity since it's the employee who knows about his desk and not vice versa.
  <p>Coming to the code part, as already mentioned above, Employee is the owner of the relation and therefore maintains a reference
  to Desk as shown below -
  </p>
  <pre class="brush: java">
@OneToOne
@JoinTable(
  name="oto_join_emp_desk",
  joinColumns = @JoinColumn(name = "emp_id"),
  inverseJoinColumns = @JoinColumn(name = "desk_id", unique = true)
)	
public Desk getDesk() {
  return desk;
}    
  </pre>
  <p>The annotations that are of interest to us are <em>@OneToOne</em> and <em>@JoinTable</em>.</p>
  <ul>
    <li><em>@OneToOne</em> -
      It marks the relationship between Employee and Desk as one-to-one association.
    </li>
    <li><em>@JoinTable</em> -
      It provides the details of the join table that will be used as intermediate table.
      <ul>
        <li><em>name</em> - It provides the name of join table.</li>
        <li><em>joinColumns</em> - It provides the name of the foreign key column in the join table that references
        primary table of the owning entity, in our case, table for Employee Entity.</li>
        <li><em>inverseJoinColumns</em> - It provides the name of the foreign key column in the join table that references
        primary table of the non-owning entity, in our case, table for Desk Entity.</li>
      </ul>
    </li>
  </ul>
  Another point to note regarding database schema is, since Employee entity is the owner in this relationship, it's
  primary key column, <em>emp_id</em>, also forms the primary key as well as foreign key of the join table. The inverse join
  column, <em>desk_id</em>, that is primary key of non-owning entity Desk needs a unique constraint to ensure One-to-One
  relationship.
   
  <h3>Secondary Table Approach</h3>
  As we have already seen in <q><em>Basic -> Secondary Table</em></q> topic, properties of an entity can be mapped to
  more that one table. This can be applied to the above mapping as shown below -
  <pre class="brush: java">
@Entity(name="OtoJoinSecondaryTableEmployee") 
@Table(name = "oto_join_sectable_emp")
@SecondaryTable(name = "oto_join_sectable_emp_desk")
public class Employee {
...
	
  @OneToOne
	@JoinColumn(table = "oto_join_sectable_emp_desk", name = "desk_id",
    unique = true)
	public Desk getDesk() {
		return desk;
	}

...
  
	@Temporal(TemporalType.DATE)
	@Column(table = "oto_join_sectable_emp_desk", nullable = false)
	public Date getAllocationDate() {
		return allocationDate;
	} 	
}       
  </pre>
  <p>Here are a few points to note about the mapping above -</p>
  <ul>
    <li><em>@SecondaryTable</em> - It provides the table name in which the employee-desk relationship
    information will be stored.</li>
    <li><em>@JoinColumn</em> - It provides the name of the foreign key column in the secondary table that
    references table of non-owning entity, in our case, it's Desk Entity.</li>
  </ul>
  <p>What could be the advantage of such mapping over the Join Table approach? Well, since it's a secondary table,
  we can have additional mapping properties put to it. For example, <em>AllocationDate</em> property that captures the
  date when an Employee was allocated a particular desk can be stored in the secondary table.
  </p>
  <p> Finally, here is how the database schema looks like for this approach - </p>
  <img src="./tables_images/oto_jointable_sectable_db.png"/>
  <p> As expected, the secondary table for Employee-Desk association uses a shared primary key with table for Employee
  entity and maintains a foreign key reference to table for Desk entity.
  </p>
</body>
</html>